﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace FanaticGene.WallPaperUtil
{
    /// <summary>
    ///     原文链接：https://blog.csdn.net/qq_39200794/article/details/119152298
    /// </summary>
    public static class User32
    {
        [DllImport("user32.dll", EntryPoint = "SetParent")]
        private static extern int SetParent(int hWndChild, int hWndNewParent);

        [DllImport("user32.dll", EntryPoint = "FindWindowA")]
        private static extern IntPtr FindWindowA(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", EntryPoint = "FindWindowExA")]
        private static extern IntPtr FindWindowExA(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll", EntryPoint = "GetClassNameA")]
        private static extern IntPtr GetClassNameA(IntPtr hWnd, IntPtr lpClassName, int nMaxCount);
        [DllImport("user32.dll", EntryPoint = "GetParent")]
        private static extern IntPtr GetParent(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        public static extern int SystemParametersInfo(int uAction, int uParam, StringBuilder lpvParam, int fuWinIni);

        /// <summary>
        /// 重置桌面。
        /// </summary>
        public static bool Refresh()
        {
            var wallpaper = new StringBuilder(200);
            SystemParametersInfo(0x73, 200, wallpaper, 0);
            var ret = SystemParametersInfo(20, 1, wallpaper, 3);
            if (ret != 0)
            {
                var hk = Registry.CurrentUser;
                var run = hk.CreateSubKey(@"Control Panel\Desktop\");
                run.SetValue("Wallpaper", wallpaper.ToString());
                return true;
            }
            return false;
        }

        //public static void SetFather(Form form)
        //{
        //    SetParent((int)form.Handle, GetBackground());
        //}

        /// <summary>
        /// 将当前的窗体句柄传到桌面。
        /// </summary>
        /// <param name="handle"></param>
        public static void ApplyHandle(int handle)
        {
            SetParent(handle, GetBackground());
        }

        private static int GetBackground()
        {
            unsafe
            {
                var background = IntPtr.Zero;
                var father = FindWindowA("progman", "Program Manager");
                var workerW = IntPtr.Zero;
                do
                {
                    workerW = FindWindowExA(IntPtr.Zero, workerW, "workerW", null);
                    if (workerW != IntPtr.Zero)
                    {
                        var buff = new char[200];
                        var b = Marshal.UnsafeAddrOfPinnedArrayElement(buff, 0);
                        var ret = (int)GetClassNameA(workerW, b, 400);
                        if (ret == 0) throw new Exception("出错");
                    }
                    if (GetParent(workerW) == father)
                    {
                        background = workerW;
                    }
                } while (workerW != IntPtr.Zero);
                return (int)background;
            }
        }


        /// <summary>
        /// 更换壁纸
        /// </summary>
        /// <param name="fileName">壁纸文件的路径</param>
        public static bool ChangeWallPaper(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return false;
            }
            if (System.IO.File.Exists(fileName) == false)
            {
                return false;
            }

            var sbfileName = new StringBuilder();
            sbfileName.Append(System.IO.Path.GetFullPath(fileName));
            var nResult = SystemParametersInfo(20, 1, sbfileName, 0x1 | 0x2); //更换壁纸
            if (nResult == 0)
            {
                return false;
            }
            else
            {
                RegistryKey hk = Registry.CurrentUser;
                RegistryKey run = hk.CreateSubKey(@"Control Panel\Desktop\");
                run.SetValue("Wallpaper", fileName);

                return true;
            }
        }

    }
}
